<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class LabStaff extends CI_Controller {
 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
 }
 function index(){ 	 	 
 	 $config['base_url'] = base_url().'labStaff/index'; 
	 $config['total_rows'] = $this->muser->num_rows('LabStaff');
	 
	 $config['per_page'] = 10; // xác định số record ở mỗi trang  
     $config['uri_segment'] = 3; // xác định segment chứa page number  
	 $this->pagination->initialize($config); 
	 $data['result']=$this->muser->getalldata('LabStaff',$config['per_page'],$this->uri->segment(3));	
	
	 $data['staff']=$this->getStaffID();
	 $data['lab']=$this->getLabID();
	 if ($error=$this->session->userdata('error')) {
	 	$data['error']=$error;
		$this->session->unset_userdata('error'); 
	 }
	 $this->load->view('_header');
     $this->load->view('labStaff/labStaff_view',$data); 
     $this->load->view('_footer');
 }
	
function insert()
	{
			$DT=array(				
				"Staff_ID"=>$this->input->post('Staff_ID'),
				"Lab_ID"=>$this->input->post('Lab_ID'),
				"Worklevel"=>$this->input->post('Worklevel'),
				"BasicSalary"=>$this->input->post('BasicSalary')
				
			);
			if ($this->check_LabStaff($DT['Staff_ID']))
			{
				$this->muser->addData('LabStaff',$DT);
				$error['error']='Insert successful!';
			}else
				{
					$error['error']=$DT['Staff_ID']. ' is existed in a Lab!';
				}
			
			$this->session->set_userdata($error);
			redirect('labStaff/index');		
	}	
 function delete($LabStaff_ID){
 	$this->muser->delColumn('LabStaff','LabStaff_ID',$LabStaff_ID);
	redirect('labStaff/index');
 }
 
 function edit1 ()
 {
 	$DT=array(
				"LabStaff_ID"=>$this->input->post('LabStaff_ID'),
				"Staff_ID"=>$this->input->post('Staff_ID'),
				"Lab_ID"=>$this->input->post('Lab_ID'),
				"Worklevel"=>$this->input->post('Worklevel'),
				"BasicSalary"=>$this->input->post('BasicSalary')				
			);
	$this->muser->updateData('LabStaff','LabStaff_ID',$DT['LabStaff_ID'],$DT);	 	
	redirect('labStaff/index');		
 } 
 function edit($str)
 {	
 	$result=$this->muser->getRdata('LabStaff','LabStaff_ID',$str);
	$data['result']=$result;
	$data['staff']=$this->getStaffID();
	$data['lab']=$this->getLabID();	
 	$this->load->view('_header');
	$this->load->view('labStaff/editLabStaff_view',$data);	
	$this->load->view('_footer');
 }
 
 function check_LabStaff($str){
 		return $this->muser->checkData('LabStaff','Staff_ID',$str);		
 }
 function getStaffID(){
 	 $lab1=$this->muser->getData('Staff');
	 foreach ($lab1 as $row) {
		 $lab[$row->Staff_ID]=$row->Staff_ID.'-'.$row->FullName;
	 }
	 return $lab;
 }
 function getLabID(){
 	 $lab1=$this->muser->getData('Lab');
	 foreach ($lab1 as $row) {
		 $lab[$row->Lab_ID]=$row->Lab_ID.'-'.$row->LabName;
	 }
	 return $lab;
 }
}